<?php
require_once('./dbfunc.php');
require_once('./dbparam.php');

header('Content-Type: json');

// 连接数据库
$conn = conn_mysql();

// 条件语句
$task_term = isset($_GET['task-term']) ? $_GET['task-term'] : null;
$ma_dep = isset($_GET['ma-dep']) ? $_GET['ma-dep'] : null;
$cl_id = isset($_GET['cl-id']) ? $_GET['cl-id'] : null;
$co_id = isset($_GET['co-id']) ? $_GET['co-id'] : null;
$te_id = isset($_GET['te-id']) ? $_GET['te-id'] : null;






// 结果参数
$cl_name = '';
$te_name = '';
$co_name = '';
$co_type = '';
$co_weekh = 0; 
$co_week = ''; 
$co_totalh = 0; 
$co_exam_type = ''; 


$response = [];


$sql = "select cl_name, te_name, co_name, co_type, co_weekh, co_week, co_totalh, co_exam_type 
                        from huangcz_course, huangcz_te, huangcz_class, huangcz_task, huangcz_major
                        where cl_id = task_class
                        and te_id = task_te
                        and task_course = co_id
                        and co_major = ma_id
                        and task_term = ?
                        ";

if ($ma_dep){
    $sql = $sql . "and ma_dep = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param('si', $task_term, $ma_dep);
}elseif ($cl_id){
    $sql = $sql . "and cl_id = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param('si', $task_term, $cl_id);
}elseif ($co_id){
    $sql = $sql . "and co_id = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param('si', $task_term, $co_id);
}elseif ($te_id){
    $sql = $sql . "and te_id = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param('si', $task_term, $te_id); 
}




$stmt->bind_result($cl_name, $te_name, $co_name, $co_type, $co_weekh, $co_week, $co_totalh, $co_exam_type);
$stmt->execute();
$stmt->store_result();

// 获取数据
while ($stmt->fetch()){
    $response[] = [
        'cl_name'=>$cl_name,
        'te_name'=>$te_name,
        'co_name'=>$co_name,
        'co_type'=>$co_type,
        'co_weekh'=>$co_weekh,
        'co_week'=>$co_week,
        'co_totalh'=>$co_totalh,
        'co_exam_type'=>$co_exam_type
    ];
}



$stmt->free_result();
exit(json_encode($response,JSON_UNESCAPED_UNICODE));
